package com.ebook.ui.fragment.home.category;

import com.ebook.database.dao.CategoryDao;
import com.ebook.entity.Category;
import com.ebook.ui.fragment.Fragment;
import com.ebook.ui.sync.WorkSyncer;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class CategoryFragment extends Fragment {
    private CategoryDao categoryDao;
    private final JLabel bookCategory = new JLabel("书籍分类");
    public CategoryFragment(){
        super();
        setLayout(null);
        setSize(340,710);
        this.setBorder(new LineBorder(Color.LIGHT_GRAY));
//        setBackground(Color.BLACK);
        super.serializeId = 11001;
        categoryDao = WorkSyncer.sp.getBean("categoryDao",CategoryDao.class);
        initComponents();
    }
    @Override
    public void initComponents(){
        bookCategory.setBounds(0,0,340,40);
        add(bookCategory);
        int y = 40;
        List<Category> categories = categoryDao.getAllFirstLevelCategory();
        for(Category category:categories){
            BookCategory bc = new BookCategory(this.getWidth(),category.getCategory_name());
            List<String> nextCategoryNames = categoryDao.getCategoryNameByPid(category.getCategory_id());
            bc.loadCategory((ArrayList<String>) nextCategoryNames);
            bc.setBounds(0,y,340,bc.getBuildHeight());
            y+= bc.getBuildHeight();
            this.add(bc);
        }
        this.setSize(340, Math.max(y, 710));//可以增强为影响整体大小
        this.setLocation(new Point(0,90));
    }
}
